package com.service.sys;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpHost;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseListener;
import org.elasticsearch.client.RestClient;

import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @Description
 * @Author tm
 * @Date 2023-06-06 10:32
 **/
@Service
public class ElasticService {
    private static RestClient restClient = null;

    public static void main(String[] args) {
        //query("",1,2,"obdgps",-1,-1);
    }


    /**
     *
     * @param dtu 设备号，可不传
     * @param page  分页 从1开始
     * @param pageSize  每数多少条
     * @param index   索引
     * @param startTime  开始时间，没有的话，传0
     * @param endTime  结束时间，没有的话，传0
     */
    public  JSONObject query(String dtu,int page,int pageSize,String index,long startTime,long endTime){
        if(restClient == null){
            restClient = RestClient.builder(
                    new HttpHost("106.52.23.94", 10200, "http")
            ).build();
        }else{
            System.out.println("））））））））））））））））=====elast用缓存的");
        }
        Request request = new Request("GET", "/" + index + "/_search/");
        request.addParameter("pretty", "true");
        request.addParameter("sort","date:desc");
        request.addParameter("from",(page-1)*pageSize+"");
        request.addParameter("size",""+pageSize);

        JSONObject jsonObject=new JSONObject();
        JSONObject query=new JSONObject();
        JSONObject bool=new JSONObject();
        JSONArray must=new JSONArray();
        if(startTime>0||endTime>0){
            JSONObject range=new JSONObject();
            JSONObject date=new JSONObject();
            if(startTime>0)
            date.put("gte",startTime);
            if(endTime>0)
            date.put("lte",endTime);
            range.put("date",date);
            JSONObject item=new JSONObject();
            item.put("range",range);
            must.add(item);
        }

        if(dtu!=null&&dtu.length()!=0){
            JSONObject match=new JSONObject();
            match.put("dtu","*"+dtu);
            JSONObject item=new JSONObject();
            item.put("wildcard",match);
            must.add(item);
        }

        if(must.size()>0){
            bool.put("must",must);
            query.put("bool",bool);
            jsonObject.put("query",query);
        }
        request.setJsonEntity(jsonObject.toString());

        try{

           Response response= restClient.performRequest(request);
           String value=EntityUtils.toString(response.getEntity());
           System.out.println(value);
           JSONObject object= JSONObject.parseObject(value);
           return object;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }


}
